<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
    <el-form-item label="物料号" prop="itemCode">
        <el-input
          v-model="queryParams.itemCode"
          placeholder="请输入物料号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="物料名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <!-- <el-form-item label="材料规格" prop="itemSpecification">
        <el-input
          v-model="queryParams.itemSpecification"
          placeholder="请输入材料规格"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <!-- <el-form-item label="材料描述" prop="itemDesc">
        <el-input
          v-model="queryParams.itemDesc"
          placeholder="请输入材料描述"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="材料图号" prop="drawingNo">
        <el-input
          v-model="queryParams.drawingNo"
          placeholder="请输入材料图号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <el-input
          v-model="queryParams.unit"
          placeholder="请输入单位"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <el-form-item label="物料类型" prop="materialType">
        <el-select
          v-model="queryParams.materialType"
          placeholder="请选择物料类型"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.OPTICORE_MATE_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="品牌" prop="brand">
        <el-input
          v-model="queryParams.brand"
          placeholder="请输入品牌"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="数量" prop="qty">
        <el-input
          v-model="queryParams.qty"
          placeholder="请输入数量"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item> -->
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          @click="openForm('create')"-->
<!--          v-hasPermi="['opticore:material-inventor:create']"-->
<!--        >-->
<!--          <Icon icon="ep:plus" class="mr-5px" /> 新增-->
<!--        </el-button>-->
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['opticore:material-inventor:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column type="selection" width="50" />
      <el-table-column label="序号" type="index" width="60" align="center" />
      <el-table-column label="物料号" prop="itemCode" align="center">
        <template #header>
          <span >物料号</span>
        </template>
      </el-table-column>
      <el-table-column label="物料名称" prop="itemName" align="center" />
      <el-table-column label="物料类型" align="center" prop="materialType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.OPTICORE_MATE_TYPE" :value="scope.row.materialType" />
        </template>
      </el-table-column>
      <el-table-column label="物料单位" prop="unit" align="center" />
      <el-table-column label="物料规格" prop="itemSpecification" align="center">
        <template #header>
          <span >物料规格</span>
        </template>
      </el-table-column>
      <el-table-column label="品牌" prop="brand" align="center">
        <template #header>
          <span >品牌</span>
        </template>
      </el-table-column>
      <el-table-column label="数量" prop="qty" align="center" />
      <el-table-column label="物料图号" prop="drawingNo" align="center" />
      <el-table-column label="操作" align="center" min-width="80">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('query', scope.row)"
            v-hasPermi="['opticore:material-inventor:query']"
          >
            查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <MaterialInventorForm ref="formRef" @success="getList" />

  <!-- 出库对话框 -->
  <el-dialog v-model="stockOutDialogVisible" title="出库操作" width="400px">
    <el-form>
      <el-form-item label="出库数量">
        <el-input-number v-model="stockOutQty" :min="1" :max="currentStockRow?.qty || 0" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="stockOutDialogVisible = false">取消</el-button>
      <el-button type="primary" @click="confirmStockOut()" :disabled="!stockOutQty">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import download from '@/utils/download'
import { MaterialInventorApi, MaterialInventorVO } from '@/api/opticore/materialinventor'
import MaterialInventorForm from './MaterialInventorForm.vue'
import {DICT_TYPE, getStrDictOptions} from "@/utils/dict";

/** 中间库 列表 */
defineOptions({ name: 'MaterialInventor' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<MaterialInventorVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  serialNo: undefined,
  batchNo: undefined,
  docNo: undefined,
  itemCode: undefined,
  itemName: undefined,
  itemSpecification: undefined,
  itemDesc: undefined,
  drawingNo: undefined,
  unit: undefined,
  materialType: undefined,
  brand: undefined,
  qty: undefined,
  mainImage: undefined,
  status: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const stockOutDialogVisible = ref(false)
const stockOutQty = ref()
const currentStockRow = ref<any>(null)

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await MaterialInventorApi.getMaterialInventorPageMid(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, row?: any) => {
  formRef.value.open(type, row)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await MaterialInventorApi.deleteMaterialInventor(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await MaterialInventorApi.exportMaterialInventorMid(queryParams)
    console.log("中间库导出数据显示",data)
    download.excel(data, '中间库.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

const handleStockOut = (row: any) => {
  currentStockRow.value = row
  stockOutQty.value = undefined
  stockOutDialogVisible.value = true
}

const confirmStockOut = async () => {
  // 这里调用出库接口，传递 currentStockRow.value.id 和 stockOutQty.value
  // await MaterialInventorApi.updateMaterialInventor({ id: currentStockRow.value.id, qty: stockOutQty.value })

  await MaterialInventorApi.updateMaterialInventor(currentStockRow.value.id)
  console.log(stockOutQty.value)
  message.success('出库成功')
  stockOutDialogVisible.value = false
  getList()
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
